//
//  ServiceCollectionCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/9.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "ServiceCollectionCell.h"
#import "ServiceCell.h"
#import "ReleaseModel.h"
#import "ServiceModel.h"
@interface ServiceCollectionCell()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataMuta;
@property(nonatomic,strong) MJRefreshNormalHeader *header;
@property(nonatomic,assign) NSInteger pageNum;
@property(nonatomic,assign) NSInteger pageSize;
@property(nonatomic,assign) NSInteger type;
@property(nonatomic,assign) NSInteger order;
@property(nonatomic,strong) NSString *industry;
@property(nonatomic,strong) NSString *address;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@end
static NSString *identifier = @"cell";
@implementation ServiceCollectionCell
-(NSMutableArray *)dataMuta
{
    if (!_dataMuta) {
        _dataMuta = [NSMutableArray array];
    }
    return _dataMuta;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, kWidth, kHeight-100) style:UITableViewStylePlain];
//        _tableView = [[UITableView alloc]initWithFrame:self.contentView.bounds style:UITableViewStylePlain];

//        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.contentView.backgroundColor = kWhite;
//        self.manager = [AFHTTPSessionManager manager];
//        self.modelAss = [[ReleaseModel alloc]init];
//        self.modelAssub = [[ReleaseModel alloc]init];
        self.manager = [AFHTTPSessionManager manager];
        [self.contentView addSubview:self.tableView];
        self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        [self.tableView registerNib:[UINib nibWithNibName:@"ServiceCell" bundle:nil] forCellReuseIdentifier:identifier];
        self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
        self.tableView.mj_header = self.header;

//        self.pageNum = 1000;
        self.pageSize = 10;
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serviceHallSearchIndustry:) name:@"serviceHallSearchIndustry" object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serviceHallSearchType:) name:@"serviceHallSearchType" object:nil];
        //address
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serviceHallSearchAddress:) name:@"serviceHallSearchAddress" object:nil];
        //serviceHallSearchOrder
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serviceHallSearchOrder:) name:@"serviceHallSearchOrder" object:nil];
    }
    return self;
}
#pragma mark-代理:行业
-(void)serviceHallSearchIndustry:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.industry = [dict objectForKey:@"industry"];
    if ([self.industry isEqualToString:@"全部"]||[self.industry isEqualToString:@"不限"]) {
        self.industry = @"";
    }
    self.pageNum = 1;
    self.dataMuta = nil;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
     [self reloadInternetData];
}
#pragma mark-代理:类型
-(void)serviceHallSearchType:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.type = [[dict objectForKey:@"type"] integerValue];
    self.pageNum = 1;
    self.dataMuta = nil;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    [self reloadInternetData];
}
#pragma mark-代理:address
-(void)serviceHallSearchAddress:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.address = [dict objectForKey:@"address"];
    if ([self.address isEqualToString:@"全国不限"]) {
        self.address = @"";
    }
    self.pageNum = 1;
    self.dataMuta = nil;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    [self reloadInternetData];
}
#pragma mark-代理:排序
-(void)serviceHallSearchOrder:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.order = [[dict objectForKey:@"order"] integerValue];
    self.pageNum = 1;
    self.dataMuta = nil;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    [self reloadInternetData];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)refresh
{
    self.dataMuta = nil;
    //    self.believeArray = nil;
    self.pageNum = 1;
    //    self.pageSize = 10;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    [self reloadInternetData];
    
}
- (void)setAssModel:(ReleaseModel *)assModel
{
    _assModel = assModel;
    self.pageNum = 1;
    self.dataMuta = nil;
    [self reloadInternetData];
}
-(void)reloadMore
{
    self.pageNum ++;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    [self reloadInternetData];
}
-(void)reloadInternetData
{

//    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
//    [WSProgressHUD show];
    NSString *types = [NSString stringWithFormat:@"%ld",self.type];
    NSString *orders = [NSString stringWithFormat:@"%ld",self.order];
    NSString *pages = [NSString stringWithFormat:@"%ld",self.pageNum];
    NSString *rows = [NSString stringWithFormat:@"%ld",self.pageSize];
    self.dict = @{
                  @"uid":@"",
                  @"associationtype":self.assModel.content,
                  @"associationtypesub1":@"",
                  @"associationtypesub2":@"",
                  @"commonindustry":@"",
                  @"industry":self.industry,
                  @"type":types,
                  @"order":orders,
                  @"page":pages,
                  @"rows":rows,
                  @"address":self.address
                  };
    XLog(@"self.dict:%@",self.dict);

    self.urlString =[NSString stringWithFormat:@"%@/association/search",kUrl];
    [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *date = [responseObject objectForKey:@"date"];
        XLog(@"date:%@",date);
        if (date.count) {
            for (NSDictionary *dict in date) {
                ServiceModel *model = [ServiceModel mj_objectWithKeyValues:dict];
                [self.dataMuta addObject:model];
            }
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
        }else {
            
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        XLog(@"self.dataMuta:%@",self.dataMuta);
        if (self.dataMuta.count) {
            MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
            self.tableView.mj_footer = footer;
                [footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"]
 forState:MJRefreshStateNoMoreData];
                footer.stateLabel.font = [UIFont systemFontOfSize:12];
                footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
            if (!date.count) {
                self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            }else if (date.count< 10) {
                self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            }
        }else {
            [self.tableView.mj_footer removeFromSuperview];
            self.tableView.emptyDataSetSource = self;
            self.tableView.emptyDataSetDelegate = self;
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [WSProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:[NSBundle bundleLocalizedStringForKey:@"footerNoInternet"]];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataMuta.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    ServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ServiceModel *model = self.dataMuta[indexPath.row];
//    cell.delegate = self;
//    cell.indexPath = indexPath;
    cell.serviceModel = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 144;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ServiceModel *model = self.dataMuta[indexPath.row];
    XLog(@"model.id:%ld",model.id);
    if ([self.delegate respondsToSelector:@selector(serviceCollectionCell:withModel:)]) {
        [self.delegate serviceCollectionCell:self withModel:model];
    }
}

@end
